<?php

namespace App\Console;

use App\Models\BorrowRecord;
use App\Models\Fine;
use App\SmsSendConn;
use App\User;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function (){
            //差5天提示提示短信
            $phone5=BorrowRecord::where('timescale','=',date('Y-m-d H:i:s',strtotime('+5day')))
                ->where('borrow_records.state','=',0)
                ->leftJoin('users as u','u.id','borrow_records.uid')
                ->leftJoin('books as b','b.bid','borrow_records.bid')
                ->select('phone','b.name','timescale','u.id')
                ->distinct('u.phone')
                ->get();

            foreach ($phone5 as $p)
            {
                if($p->phone==null)
                {
                    continue;
                }

                $url = 'http://api01.monyun.cn:7901/sms/v2/std/';
                $smsSendConn = new SmsSendConn($url);
                $data=array();
                $data['userid']='E105VN';
                $data['pwd']='86aDoI';
                $data['mobile']=$p->phone;

                $data['content']='您借阅的图书将于5天后过期，请按时归还！超时罚款 1 元/天。';
                try {
                    $result = $smsSendConn->singleSend($data);
                    if ($result['result'] === 0) {
                        print_r("单条信息发送成功！");
                    } else {
                        print_r("单条信息发送失败，错误码：" . $result['result']);
                    }
                }catch (\PHPUnit\Framework\Exception $e) {
                    print_r($e->getMessage());//输出捕获的异常消息，请根据实际情况，添加异常处理代码
                }
            }

            //每天超期提醒
            $phoneo=BorrowRecord::where('timescale','<',date('Y-m-d H:i:S',time()))
                ->where('borrow_records.state','=',1)
                ->leftJoin('users as u','u.id','borrow_records.uid')
                ->leftJoin('books as b','b.bid','borrow_records.bid')
                ->select('phone','b.name','timescale')
                ->get();

            foreach ($phoneo as $p)
            {
                if($p->phone==null)
                {
                    continue;
                }

                $timescale=strtotime($p->timescale);
                $today=strtotime(date('Y-m-d H:i:S',time()));
                $diff=floor(($today - $timescale) / 86400);

                $url = 'http://api01.monyun.cn:7901/sms/v2/std/';
                $smsSendConn = new SmsSendConn($url);
                $data=array();
                $data['userid']='E105VN';
                $data['pwd']='86aDoI';
                $data['mobile']=$p->phone;
                $data['content']='您借阅的图书已超时'.$diff.'天，请尽快归还！超时罚款 1元/天';
                try {
                    $result = $smsSendConn->singleSend($data);
                    if ($result['result'] === 0) {
                        print_r("单条信息发送成功！");
                    } else {
                        print_r("单条信息发送失败，错误码：" . $result['result']);
                    }
                }catch (\PHPUnit\Framework\Exception $e) {
                    print_r($e->getMessage());//输出捕获的异常消息，请根据实际情况，添加异常处理代码
                }
            }

        })->dailyAt("11:00");

        $schedule->call(function (){
            $borrow_id=BorrowRecord::where('timescale','<=',date('Y-m-d',time()))
                  ->where('state',0)
                  ->get();
            BorrowRecord::where('timescale','<=',date('Y-m-d',time()))
                ->where('state',0)
                ->update([
                   'state'=>1
                ]);
            foreach ($borrow_id as $b)
            {
                Fine::create([
                    'borrow_id'=>$b->borrow_id,
                    'price'=>1,
                    'is_paid'=>0
                ]);
            }

            Fine::where('is_paid',0)
                ->increment('price',1);
            $over_timescale_users=BorrowRecord::where('state',1)->get();

            foreach ($over_timescale_users as $over_timescale_user)
            {
                User::where('id',$over_timescale_user->uid)->update(['state'=>1]);
            }

        })->dailyAt("19:55");

    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
//        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
